package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.AdCreativeRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.AdCreative;
import com.ndp.fb.util.SQLUtil;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Created by hunk.qin on 2015/3/12.
 */
@Service
public class AdCreativeRdbServiceImpl extends DaoTemplate implements AdCreativeRdbService {

    @Override
    public Long save(final AdCreative adCreative) {
        //local_updated_time 不处理，默认是0。 有问题@豪哥。
        return saveResultId(adCreative);
    }


    /**
     * @param adCreativeId
     * @return
     * @discription
     * @author yanghao 创建时间 2015年3月20日 上午11:44:22
     * @see com.ndp.fb.rdb.api.AdCreativeRdbService#findByAdCreativeId(java.lang.Long)
     */

    @Override
    public AdCreative findByAdCreativeId(Long adCreativeId) {
        String sql = "SELECT * FROM ad_creative WHERE ad_creative_id = ?";
        List<AdCreative> adCreatives = getJdbcTemplate().query(sql,
                new Object[]{adCreativeId}, DefaultRowMapper.autoRowMapper(AdCreative.class));
        if (CollectionUtils.isEmpty(adCreatives)) {
            return null;
        } else {
            return adCreatives.get(0);
        }

    }

    @Override
    public List<String> filterUseMd5s(List<String> md5s) {
        String sql = "SELECT a.imageMd5 from ad_creative a where a.imageMd5 in ("+SQLUtil.appendPlaceholder(md5s.size())+")\n" +
                "group by a.imageMd5 HAVING count(1) > 0";
        return getJdbcTemplate().queryForList(sql,md5s.toArray(),String.class);
    }


    @Override
    public void updateEcTemplateMaterielId(Long creativeId, Long ecTemplateMaterielId) {
        String sql = "UPDATE ad_creative SET ec_template_materiel_id = ? WHERE ad_creative_id=?";
        getJdbcTemplate().update(sql, new Object[]{ecTemplateMaterielId, creativeId});
    }

}
